﻿using System.Collections.Generic;
using System.Data;
using personalplaner.business.common;
using personalplaner.business.enums;
using personalplaner.business.stamm;
using System;
using personalplaner.business.extensions;

namespace personalplaner.dataprovider.services
{
    public partial class PersonDataService
    {
        internal static IEnumerable<Person> m_personenData(DatabaseSettings settings, DataTable table)
        {
            var showPrsId = (int)DataServices.OptionsService.NumberValue(settings, enOptions.DisplayOptions, 4) == 1;
            var namedisplay = (enNameDisplay)(int)DataServices.OptionsService.NumberValue(settings, enOptions.DisplayOptions, 5);

            bool prsidExists = table.Columns.Contains("prsid");
            bool vornameExists = table.Columns.Contains("vorname");
            bool nachnameExists = table.Columns.Contains("nachname");
            bool strasseExists = table.Columns.Contains("strasse");
            bool stnrExists = table.Columns.Contains("stnr");
            bool postfachExists = table.Columns.Contains("postfach");
            bool plzExists = table.Columns.Contains("plz");
            bool ortExists = table.Columns.Contains("ort");
            bool telpExists = table.Columns.Contains("telp");
            bool telnExists = table.Columns.Contains("teln");
            bool emailExists = table.Columns.Contains("email");
            bool typExists = table.Columns.Contains("typ");

            foreach (DataRow row in table.Rows)
            {
				var typ = enPersonTyp.None;
				if (typExists)
				{
					switch (row.Field<int>("typ"))
					{
						case 0:
							typ = enPersonTyp.Alle;
							break;
						case 1:
							typ = enPersonTyp.Stammperson;
							break;
						case 2:
							typ = enPersonTyp.Zusatzperson;
							break;
						case 3:
							typ = enPersonTyp.Inaktiv;
							break;
					}
				}

            	yield return new Person
                {
                    ID = prsidExists ? row.Field<int>("prsid") : int.MaxValue,
                    PrsID = prsidExists ? row.Field<int>("prsid") : 0,
					Name = vornameExists ? row.Field<string>("vorname").TrimNullable() : string.Empty,
					Nachname = nachnameExists ? row.Field<string>("nachname").TrimNullable() : string.Empty,
					Strasse = strasseExists ? row.Field<string>("strasse").TrimNullable() : string.Empty,
					StrassenNr = stnrExists ? row.Field<string>("stnr").TrimNullable() : string.Empty,
                    Postfach = postfachExists ? row.Field<string>("postfach") : string.Empty,
                    PLZ = plzExists ? row.Field<string>("plz").TrimNullable() : string.Empty,
					Ort = ortExists ? row.Field<string>("ort").TrimNullable() : string.Empty,
					TelefonPrivat = telpExists ? row.Field<string>("telp").TrimNullable() : string.Empty,
					Natel = telnExists ? row.Field<string>("teln").TrimNullable() : string.Empty,
					EMail = emailExists ? row.Field<string>("email").TrimNullable() : string.Empty,
                    PersonTyp = typ,
                    ShowPrsId = showPrsId,
                    NameDisplay = namedisplay
                };
            }
        }


		internal static IEnumerable<Holiday> m_holidayData(DataTable table)
		{
			bool ferienidExists = table.Columns.Contains("ferienid");
			bool prsidExists = table.Columns.Contains("prsid");
			bool vonExists = table.Columns.Contains("von");
			bool bisExists = table.Columns.Contains("bis");

			foreach (DataRow row in table.Rows)
			{
				yield return new Holiday
				{
					ID = ferienidExists ? row.Field<int>("ferienid") : int.MaxValue,
					Name = "Holiday",
					FerienId = ferienidExists ? row.Field<int>("ferienid") : 0,
					PrsId = prsidExists ? row.Field<int>("prsid") : 0,
					Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
					Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue
				};
			}
		}
	}
}
